package com.haofengsoft.scce;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.haofengsoft.platform.utils.StringUtil;
import com.haofengsoft.scce.common.client.VolleyBean;
import com.haofengsoft.scce.common.client.VolleyInterface;
import com.haofengsoft.scce.common.client.VolleyManage;
import com.haofengsoft.scce.common.config.ConstantConfig;
import com.haofengsoft.scce.controller.UserController;

/**
 * 设置密码
 *
 * @author zhj
 */
public class SetPassWordActivity extends Activity {

    private EditText pwdEt;// 密码
    private EditText confirmPwdEt;// 确认密码
    private String account;// 注册的用户（手机号）

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.set_password);
        AppManage.getInstance().addActivity(SetPassWordActivity.this);
        account = getIntent().getStringExtra("account");
        pwdEt = (EditText) findViewById(R.id.setpassword_password_ed);
        confirmPwdEt = (EditText) findViewById(R.id.setpassword_confirmpassword_et);
        ImageView backIv = (ImageView) findViewById(R.id.setpassword_back_iv);
        backIv.setOnClickListener(new BackClickListener());
        Button nextStepBtn = (Button) findViewById(R.id.setpassword_next_step_btn);
        nextStepBtn.setOnClickListener(new NextstepClickListener());
    }

    /**
     * 下一步
     *
     * @author zhj
     */
    class NextstepClickListener implements OnClickListener {

        @Override
        public void onClick(View view) {
            String pwd = pwdEt.getText().toString();
            String confirmPwd = confirmPwdEt.getText().toString();
            if (StringUtil.isEmpty(pwd)) {
                Toast.makeText(SetPassWordActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                return;
            }
            if (StringUtil.isEmpty(confirmPwd)) {
                Toast.makeText(SetPassWordActivity.this, "请确认密码", Toast.LENGTH_SHORT).show();
                return;
            }
            if (!pwd.equals(confirmPwd)) {
                Toast.makeText(SetPassWordActivity.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                return;
            }
            Map<String, Object> mapParam = new HashMap<String, Object>();
            mapParam.put("UserAccount", account);
            mapParam.put("NewPassword", pwd);
            VolleyManage.getInstance().methodPost(SetPassWordActivity.this, "处理中...", ConstantConfig.REGISTER_USER_URL, mapParam, new SetPassVolleyReal());
        }

    }

    class SetPassVolleyReal implements VolleyInterface {

        @Override
        public void gainData(VolleyBean bean) {
            if (bean.isSuccess()) {
                //个信息
                boolean result = UserController.getInstance(SetPassWordActivity.this).handUser(bean.getContent());
                if (result) {
                    AppManage.getInstance().finishActivity(RegistActivity.class);
                    AppManage.getInstance().finishActivity(LoginActivity.class);
                    finish();
                } else {
                    Toast.makeText(SetPassWordActivity.this, "请重新设置密码", Toast.LENGTH_SHORT).show();
                }

            }
        }
    }

    class BackClickListener implements OnClickListener {

        @Override
        public void onClick(View view) {
            finish();
        }

    }
}
